import 'package:flutter/material.dart';
import 'package:flutterapp/page/utils/common_picker/index.dart';
import 'package:flutterapp/page/widgets/common_form_item.dart';

class CommonRadioFormItem extends StatelessWidget {
  final String label;
  final List<String> options;
  final int value;
  final ValueChanged<int> onChange;

  const CommonRadioFormItem(
      {Key key, this.label, this.options, this.value, this.onChange})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CommonFormItem(
      label: label,
      contentBuild: (context) {
        return GestureDetector(
          onTap: () {
            var result = CommonPicker.showPicker(
                context: context, options: options, value: value);

            result.then((selectedValue) {
              if (value != selectedValue &&
                  selectedValue != null &&
                  onChange != null) {
                onChange(selectedValue);
              }
            });
          },
          child: Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,

              //这个遍历可以获取索引
              children: List.generate(
                  options.length,
                  //index索引   map便利item是值
                  (index) => Padding(
                        padding: EdgeInsets.only(left: 0.0),
                        child: Row(
                          children: [
                            Container(
                              margin: EdgeInsets.only(right: 0.0),
                              child: Radio(
                                  value: index,
                                  groupValue: value,
                                  onChanged: onChange),
                            ),
                            Text(options[index])
                          ],
                        ),
                      )),
            ),
          ),
        );
      },
    );
  }
}
